package com.example.chao.myphotosearch.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.example.chao.myphotosearch.adapters.holders.BaseViewHolder;
import com.example.chao.myphotosearch.adapters.holders.SearchHolder;
import com.example.chao.myphotosearch.bean.SosoImage;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by chao on 2016/4/7.
 */
public class SearchAdapter extends RecyclerView.Adapter<BaseViewHolder<SosoImage>> {
    private ArrayList<SosoImage> sosoImages;
    private OnSearchItemClickListener listener;
    @Inject
    public SearchAdapter() {
        sosoImages = new ArrayList<>();
    }

    public void setListener(OnSearchItemClickListener listener) {
        this.listener = listener;
    }

    public void setSosoImages(List<SosoImage> sosoImages) {
        if(sosoImages!=null) {
            this.sosoImages.clear();
            this.sosoImages.addAll(sosoImages);
            notifyDataSetChanged();
        }
    }

    @Override
    public SearchHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SearchHolder(parent);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder<SosoImage> holder, final int position) {
        holder.setData(sosoImages.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(listener!=null){
                   listener.onSearchItemClick(position,sosoImages);
               }
            }
        });
    }

    @Override
    public int getItemCount() {
        return sosoImages.size();
    }

    public interface OnSearchItemClickListener{
        void onSearchItemClick(int position,ArrayList<SosoImage> list);
    }
}
